package com.ruoyi.web.controller.system;

import java.util.List;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.system.domain.SysSuppliernper;
import com.ruoyi.system.service.ISysSuppliernperService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysSupplier;
import com.ruoyi.system.service.ISysSupplierService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 供应商信息Controller
 * 
 * @author ruoyi
 * @date 2020-12-13
 */
@Controller
@RequestMapping("/system/supplier")
public class SysSupplierController extends BaseController
{
    private String prefix = "system/supplier";

    @Autowired
    private ISysSupplierService sysSupplierService;
    @Autowired
    private ISysSuppliernperService sysSuppliernperService;
    @Autowired
    private SysPasswordService passwordService;

    @RequiresPermissions("system:supplier:view")
    @GetMapping()
    public String supplier()
    {
        return prefix + "/supplier";
    }

    /**
     * 查询供应商信息列表
     */
    @RequiresPermissions("system:supplier:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysSupplier sysSupplier)
    {
        startPage();
        List<SysSupplier> list = sysSupplierService.selectSysSupplierList(sysSupplier);
        return getDataTable(list);
    }

    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:supplier:list")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SysSupplier> util = new ExcelUtil<SysSupplier>(SysSupplier.class);
        List<SysSupplier> userList = util.importExcel(file.getInputStream());
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = sysSupplierService.importUser(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    @RequiresPermissions("system:supplier:list")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<SysSupplier> util = new ExcelUtil<SysSupplier>(SysSupplier.class);
        return util.importTemplateExcel("供应商数据");
    }
    /**
     * 导出供应商信息列表
     */
    @RequiresPermissions("system:supplier:export")
    @Log(title = "供应商信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysSupplier sysSupplier)
    {
        List<SysSupplier> list = sysSupplierService.selectSysSupplierList(sysSupplier);
        ExcelUtil<SysSupplier> util = new ExcelUtil<SysSupplier>(SysSupplier.class);
        return util.exportExcel(list, "supplier");
    }

    /**
     * 新增供应商信息
     */
    @GetMapping("/add/{supplierNperId}")
    public String add(@PathVariable("supplierNperId") Long supplierNperId, ModelMap mmap)
    {
        SysSuppliernper sysSuppliernper = sysSuppliernperService.selectSysSuppliernperById(supplierNperId);
        mmap.put("supplierNperId", sysSuppliernper.getSupplierNperId());
        mmap.put("textValue",sysSuppliernper.getTitle());
        return prefix + "/add";
    }

    /**
     * 新增保存供应商信息
     */
    @RequiresPermissions("system:supplier:add")
    @Log(title = "供应商信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysSupplier sysSupplier)
    {
        sysSupplier.setSalt(ShiroUtils.randomSalt());
        sysSupplier.setPassword(passwordService.encryptPassword(sysSupplier.getAccount(), sysSupplier.getPassword(), sysSupplier.getSalt()));
        sysSupplier.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(sysSupplierService.insertSysSupplier(sysSupplier));
    }

    /**
     * 修改供应商信息
     */
    @GetMapping("/edit/{supplierId}")
    public String edit(@PathVariable("supplierId") Long supplierId, ModelMap mmap)
    {
        SysSupplier sysSupplier = sysSupplierService.selectSysSupplierById(supplierId);
        SysSuppliernper sysSuppliernper = sysSuppliernperService.selectSysSuppliernperById(sysSupplier.getSupplierNperId());
        mmap.put("textValue",sysSuppliernper.getTitle());
        mmap.put("sysSupplier", sysSupplier);
        return prefix + "/edit";
    }

    /**
     * 修改保存供应商信息
     */
    @RequiresPermissions("system:supplier:edit")
    @Log(title = "供应商信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysSupplier sysSupplier)
    {
        return toAjax(sysSupplierService.updateSysSupplier(sysSupplier));
    }

    /**
     * 删除供应商信息
     */
    @RequiresPermissions("system:supplier:remove")
    @Log(title = "供应商信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysSupplierService.deleteSysSupplierByIds(ids));
    }

    /**
     * 供应商重置密码
     * @param supplierId
     * @param mmap
     * @return
     */
    @RequiresPermissions("system:user:resetPwd")
    @GetMapping("/resetPwd/{supplierId}")
    public String resetPwd(@PathVariable("supplierId") Long supplierId, ModelMap mmap)
    {
        mmap.put("supplier", sysSupplierService.selectSysSupplierById(supplierId));
        return prefix + "/resetPwd";
    }
    @RequiresPermissions("system:supplier:resetPwd")
    @Log(title = "重置密码", businessType = BusinessType.UPDATE)
    @PostMapping("/resetPwd")
    @ResponseBody
    public AjaxResult resetPwdSave(SysSupplier sysSupplier)
    {

        sysSupplier.setSalt(ShiroUtils.randomSalt());
        sysSupplier.setPassword(passwordService.encryptPassword(sysSupplier.getAccount(), sysSupplier.getPassword(), sysSupplier.getSalt()));
        if (sysSupplierService.resetUserPwd(sysSupplier) > 0)
        {

            return success();
        }
        return error();
    }



}
